<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Report extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		$this->load->helper(array('form', 'url'));
		$this->load->config('publisher', TRUE);
		$this->load->config('adsense', TRUE);
		$this->load->library('security');
		$this->load->library('session');
		$this->load->library('soapclientnusoap');
	}

	function index()
	{	
		if (!$this->tank_auth->is_logged_in()) { // logged in
            redirect('/auth/login');
        }
		else{
			redirect('/backyard/show_publishers');
		}
	}
	
	function generate_report(){
		$server = $this->config->item('API_server', 'adsense');
		$namespace = 'http://www.google.com/api/adsense/v3';
		
		// Set up the authentication headers
		$email = "<developer_email>".$this->config->item('developer_email', 'adsense')."</developer_email>";
		$password = "<developer_password>".$this->config->item('developer_password', 'adsense')."</developer_password>";
		$client_id = "<client_id>ca-pub-6084419821077176</client_id>";
		$header = $email . $password . $client_id;
		// creating soap client
		$wsdl = $server . '/api/adsense/v3/ReportService?wsdl';
		$client = new soapclientnusoap($wsdl, true);
		$client->soap_defencoding = 'UTF-8';
		$client->setHeaders($header);
		// setting the parameter
			$report ='<dateRange>';
				//$report .='<endDate></endDate>';
				$report .='<isPredefined type="xsd:boolean">true</isPredefined>';
				$report .='<predefinedDateRange><value>Yesterday</value></predefinedDateRange>';
				//$report .='<startDate></startDate>';
			$report .='</dateRange>';
			$report .='<outputFormat>';
				$report .= '<value>CSV</value>';
			$report .='</outputFormat>';
			$report .='<unit>';
				$report .= '<value>Page</value>';
			$report .='</unit>';
		$param = "<ns1:generateReport xmlns:ns1=\"$namespace\"><report xsi:type=\"ns:AFCAggregateReport\">".$report."</report></ns1:generateReport>";
		$response = $client->call('generateReport', $param, $namespace);
		$this->showRequestResponse($client);
		echo "<pre>";print_r($response);echo "</pre>";
		
	}
	
	function get_bulk_approval_status($publishers){
		$server = $this->config->item('API_server', 'adsense');
		$namespace = 'http://www.google.com/api/adsense/v3';
		
		// Set up the authentication headers
		$email = "<developer_email>".$this->config->item('developer_email', 'adsense')."</developer_email>";
		$password = "<developer_password>".$this->config->item('developer_password', 'adsense')."</developer_password>";
		$client_id = "<client_id>".$this->config->item('developer_client_id', 'adsense')."</client_id>";
		$header = $email . $password . $client_id;
		// creating soap client
		$wsdl = $server . '/api/adsense/v3/AccountService?wsdl';
		$client = new soapclientnusoap($wsdl, true);
		$client->soap_defencoding = 'UTF-8';
		$client->setHeaders($header);
		// setting the parameter
		$clientIds = '';
		foreach($publishers as $child_key=>$child_val){
			if(!empty($child_val['googleID_ContentAds'])){
				$clientIds .= '<clientIds>'.$child_val['googleID_ContentAds'].'</clientIds>';
			}
			elseif(!empty($child_val['googleID_SearchAds'])){
				$clientIds .= '<clientIds>'.$child_val['googleID_SearchAds'].'</clientIds>';	
			}
		}
		$param = "<ns1:getBulkApprovalStatus xmlns:ns1=\"$namespace\">".$clientIds."</ns1:getBulkApprovalStatus>";

		// invoke web service
		$response = $client->call('getBulkApprovalStatus', $param, $namespace);
		//$this->showRequestResponse($client);
		$response = $response['return'];
		
		foreach($response as $key=>$val){
			$publishers['approvalStatus'][$val['clientId']] = $val['approvalStatus']['value'];
		}
		//echo "<pre>";print_r($publishers);echo "</pre>";
		return $publishers;
	}

	function get_bulk_association_status($publishers){
		$server = $this->config->item('API_server', 'adsense');
		$namespace = 'http://www.google.com/api/adsense/v3';
		
		// Set up the authentication headers
		$email = "<developer_email>".$this->config->item('developer_email', 'adsense')."</developer_email>";
		$password = "<developer_password>".$this->config->item('developer_password', 'adsense')."</developer_password>";
		$client_id = "<client_id>".$this->config->item('developer_client_id', 'adsense')."</client_id>";
		$header = $email . $password . $client_id;
		// creating soap client
		$wsdl = $server . '/api/adsense/v3/AccountService?wsdl';
		$client = new soapclientnusoap($wsdl, true);
		$client->soap_defencoding = 'UTF-8';
		$client->setHeaders($header);
		// setting the parameter
		$clientIds = '';
		foreach($publishers as $child_key=>$child_val){
			if(!empty($child_val['googleID_ContentAds'])){
				$clientIds .= '<clientIds>'.$child_val['googleID_ContentAds'].'</clientIds>';
			}
			elseif(!empty($child_val['googleID_SearchAds'])){
				$clientIds .= '<clientIds>'.$child_val['googleID_SearchAds'].'</clientIds>';	
			}
		}
		$param = "<ns1:getBulkAssociationStatus xmlns:ns1=\"$namespace\">".$clientIds."</ns1:getBulkAssociationStatus>";

		// invoke web service
		$response = $client->call('getBulkAssociationStatus', $param, $namespace);
		//$this->showRequestResponse($client);
		$response = $response['return'];
		
		foreach($response as $key=>$val){
			$publishers['associationStatus'][$val['clientId']] = $val['associationStatus']['value'];
		}
		
		return $publishers;
	}
	
	function showSnippet($snippet) {
	  $text = $this->toHtml($snippet, ENT_QUOTES);
	  return $text;
	}
	function showSoapClientError($err) {
	  echo '<h2>Soap Client Error</h2>' . "\n";
	  echo '<pre>' . $err . '</pre>';
	  echo "\n";
	}
	function showMyErrors($client) {
	  // print the soap
	  $this->showRequestResponse($client);
	
	  // print the error information
	  echo '<h2>Fault Code</h2>' . "\n";
	  echo "<b>Fault:</b>  {$client->fault}<br>\n";
	  echo "<b>Code:</b> {$client->faultcode}<br>\n";
	  echo "<b>String:</b> {$client->faultstring}<br>\n";
	  echo "<b>Detail:</b> {$client->faultdetail}<br>\n";
	}
	

	function toHtml($text)
	{
	  $my_text = htmlspecialchars($text, ENT_QUOTES);
	  $remove = array ("&lt;", " xmlns");
	  $insert = array ("<br>&lt;", "<br>    xmlns");
	  $my_text = str_replace($remove, $insert, $my_text);
	  return $my_text;
	}
	
	function showRequestResponse($client) {
	  // print request
	  echo '<h2>Request</h2>' . "\n";
	  echo '<pre>' . $this->toHtml($client->request) . '</pre>';
	  echo "\n";
	
	  // print response
	  echo '<h2>Response</h2>'. "\n";
	  echo '<pre>' . $this->toHtml($client->response) . '</pre>';
	  echo "\n";
	
	  // Uncomment the following lines to print debug statements
	  /*
	  echo '<h2>Debug</h2>' . "\n";
	  echo '<pre>' . toHtml($client->debug_str) . '</pre>';
	  echo "\n";
	  */
	}
}

/* End of file backyard.php */
/* Location: ./application/controllers/backyard.php */